package com.sheledon.flyCat.common.pojo;

import com.sheledon.flyCat.network.socket.http.enum_.HttpMethod;
import lombok.Builder;
import lombok.Data;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Data
public class MethodDetail {
    private Method targetMethod;
    private Map<String,String> queryParamsMap=new HashMap<>();
    private Map<String,String> urlParamsMap=new HashMap<>();
    private String json;

    public void build(String requestPath,Map<String,Method> requestMappings,Map<String,String> routeMap){
        routeMap.forEach((pattern,routes)->{
            if (requestPath.matches(pattern)){
                this.setTargetMethod(requestMappings.get(pattern));
                this.setUrlParamsMap(requestPath,routes);
                return;
            }
        });
    }

    /**
     *   requestPath : /user/sheledon/wuliang/11
     *   pattern     : /user/[\\u4e00-\\u9fa5_a-zA-Z0-9]+/wuliang/[\\u4e00-\\u9fa5_a-zA-Z0-9]+/?$
     *   routes      : /user/{name}/wuliang/{id}
     */
    public void setUrlParamsMap(String requestPath,String routes){
        String[] split = routes.split("/");
        String [] rps = requestPath.split("/");
        for (int i = 0; i < split.length; i++) {
            if (split[i].startsWith("{")&&split[i].endsWith("}")){
                urlParamsMap.put(split[i].substring(1,split[i].length()-1),rps[i]);
            }
        }
    }
}
